<template>
  <div class="app-container">
    <div class="queryform">
      <el-form
        :model="form"
        ref="form"
        label-width="97px"
        label-position="right"
      >
        <el-form-item label="决策点名称:" prop="decisionPointId">
          <el-select v-model="form.decisionPointId" clearable>
            <el-option
              v-for="item in decisionPointList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="信任类型:" prop="trustType">
          <el-select v-model="form.trustType" clearable>
            <el-option
              v-for="item in xinrenList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="资源名称:" prop="resourceName">
          <el-input
            v-model.trim="form.resourceName"
            maxlength="128"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item> -->
        <!-- <el-form-item label="下发策略地址:" prop="policyUrl">
          <el-input
            v-model.trim="form.policyUrl"
            maxlength="128"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" @click="refresh">查询</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 操作栏 -->
    <div class="buttons">
      <el-button type="primary" icon="el-icon-plus" @click="add"
        >新增</el-button
      >
    </div>
    <div class="mod-result-table">
      <HTable
        ref="table"
        :tableData="tableData"
        :columnConfigs="columnConfigs"
        @selection-change="handleSelectionChange"
        :selectable="false"
        orderable
        v-loading="loading"
        :pages="pagination"
        @page-change="handlePageChange"
        @size-change="handleSizeChange"
      >
        <template slot-scope="{ row }">
          <template v-if="row.name == 'action'">
            <el-button
              type="text"
              size="small"
              icon="el-icon-document"
              @click="sedetail(row.row)"
              >查看策略</el-button
            >
            <el-button
              type="text"
              size="small"
              icon="el-icon-edit"
              @click="edit(row.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              icon="el-icon-delete"
              size="small"
              @click="handleDelete(row.row)"
              >删除</el-button
            >
          </template>
          <template v-else-if="row.name == 'color'">
            <el-tag
              :style="{
                color: '#fff',
                backgroundColor: row.row.color,
                borderColor: row.row.color,
              }"
              effect="plain"
            >
              {{ row.row.color }}
            </el-tag>
          </template>
          <template v-else-if="row.name == 'trustType'">
            <!-- {{ $getOptionLabel(row.row.trustType, xinrenList) }} -->
            {{ gettrustTypeabel(row.row.policy[0]) }}
            <!-- {{ row.row.policy[0].trustAtrr[0].trustType }} -->
          </template>
          <template v-else-if="row.name == 'rotation'">
            {{ row.row.rotation && row.row.rotation + "°" }}
          </template>
          <template v-else-if="row.name == 'transparency'">
            {{ row.row.transparency && formatTrancy(row.row.transparency) }}
          </template>
        </template>
      </HTable>
    </div>
    <div class="vpnform">
      <addEdit
        ref="AddDialog"
        :dialogVisible1.sync="dialogVisible1"
        :editFlag.sync="editFlag"
        @submit="submitaddForm"
      ></addEdit>
    </div>
    <div class="vpnform">
      <detail
        ref="Detail"
        :detailVisible.sync="detailVisible"
        :editFlag.sync="detailFlag"
        @submit="submitaddForm"
      ></detail>
    </div>
  </div>
</template>

<script>
import table from "@/mixins/commontable";
import addEdit from "./addoredit/add.vue";
import detail from "./detail/index.vue";
import { columnConfigs } from "./columnConfigs.js";
import { getList, delCurRow } from "@/api/yybl/decision";
import { validateIpPort } from "@/utils/validate";
import { mapState } from "vuex";
export default {
  components: {
    addEdit,
    detail,
  },
  mixins: [table], //表格的混入，主要包括表格上通用的方法和数据，比如查询，重置，分页这些功能
  props: {},
  data() {
    let checkTransparency = (rex, value, callback) => {
      // if (!value) {
      //   return callback(new Error('年龄不能为空'));
      // }
      // const rule = new RegExp("^0.\d$");
      const rule = /^0.\d+$/;
      if (rule.test(value) || value == 0 || value == 1) {
        callback();
      } else {
        callback(new Error("透明度范围为0-1"));
      }
    };
    let checkNumber = (rex, value, callback) => {
      // if (!value) {
      //   return callback(new Error('年龄不能为空'));
      // }
      // const rule = new RegExp("^0.\d$");
      const rule = /^\d*$/;
      if (rule.test(value)) {
        callback();
      } else {
        callback(new Error("请输入数字"));
      }
    };
    return {
      colors: {
        // hex: "#194d33",
        // hex8: "#194D33A8",
        // hsl: { h: 150, s: 0.5, l: 0.2, a: 1 },
        // hsv: { h: 150, s: 0.66, v: 0.3, a: 1 },
        // rgba: { r: 25, g: 77, b: 51, a: 1 },
        // a: 1,
      },
      rules: [
        {
          label: "完全匹配",
          value: "0",
        },
        {
          label: "频次匹配",
          value: "1",
        },
      ],
      zhouqis: [
        {
          label: "月",
          value: "1",
        },
        {
          label: "周",
          value: "2",
        },
        {
          label: "日",
          value: "3",
        },
      ],
      columnConfigs,
      dialogVisible1: false,
      editFlag: false,
      detailFlag: false,
      detailVisible: false,
      curRow: {},
      form: {
        decisionPointId: "",
        trustType: "",
        resourceName: "",
        policyUrl: "",
      },
    };
  },
  watch: {},
  computed: {
    ...mapState("common", ["xinrenList", "decisionPointList"]),
  },
  methods: {
    handleChange(val) {
      console.log(val);
    },
    add() {
      this.editFlag = false;
      this.dialogVisible1 = true;
      // debugger
      this.$nextTick(() => {
        this.$refs.AddDialog.add();
      });
    },
    edit(row) {
      this.editFlag = true;
      this.dialogVisible1 = true;
      this.curRow = row;
      this.$refs.AddDialog.edit(row);
    },
    sedetail(row) {
      this.detailFlag = true;
      this.detailVisible = true;
      this.curRow = row;
      this.$refs.Detail.seedetail(row);
    },
    async handleDelete(row) {
      this.$confirm("此操作将删除数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let ids = [];
          if (row) {
            ids.push(row.decisionPointId);
          }
          const res = await delCurRow({
            ids: ids,
          });
          if (res.rescode == "200") {
            this.$message.success("删除成功");
            this.refresh();
          } else this.$message.error(res.msg);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    submitaddForm(val) {
      if (val == 200) {
        this.refresh();
      }
    },
    handleClose() {
      this.dialogVisible1 = false;
    },
    handleSelectionChange() {},
    getparams() {
      const { decisionPointId, trustType, resourceName, policyUrl } = this.form;
      return {
        decisionPointId,
        trustType,
        resourceName,
        policyUrl,
      };
    },
    refresh() {
      this.getlists(getList, this.getparams());
    },
    resetForm() {
      this.$nextTick(() => {
        this.$refs["form"].resetFields();
        this.refresh();
      });
    },
    gettrustTypeabel(val){
      return val.trustAttr[0].trustType
    }
  },
  created() {
    this.refresh();
  },
  mounted() {},
};
</script>
<style lang="scss" scoped>
::v-deep .el-form-item__content {
  .el-date-editor.el-input {
    width: 100%;
  }
}
::v-deep .el-button {
  .el-icon-document + span {
    margin-left: 0px;
  }
  .el-icon-edit + span {
    margin-left: 0px;
  }
  .el-icon-delete + span {
    margin-left: 0px;
  }
}
::v-deep .el-dialog {
  min-width: 640px;
  .el-dialog__header {
    border-bottom: 1px solid #e8e8e8;
  }
  .el-dialog__footer {
    border-top: 1px solid #e8e8e8;
  }
}
.addDialog {
  .el-dialog {
    min-width: 640px;
  }

  .el-form {
    .el-form-item.slider {
      width: 100%;
    }
  }
}
.queryform {
  ::v-deep .el-form {
    display: flex;
    flex-wrap: wrap;

    .el-form-item {
      max-width: 33.3%;
      flex-grow: 1;
      flex-shrink: 1;
      flex-basis: 33.3%;
    }
  }
}
</style>
